package july.android.cakes.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import july.android.cakes.service.SMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author S_Cake
 * @version 失物招领
 */
@RestController
@EnableAutoConfiguration
public class CMessage {

    @Autowired
    private SMessage sv;


    @RequestMapping("/UP_lostfound")
    public void do_UPlostfound(@RequestParam("imgURL") String pic, @RequestParam("ttype") String ttype, @RequestParam("detail") String detail, @RequestParam("tel") String tel, @RequestParam("stu_id") String stu_id, HttpServletResponse hp) throws IOException {
        sv.UPlostfound(pic, ttype, detail, tel, stu_id);
        //System.out.println(ttype);
        //System.out.println(detail);
        //System.out.println(tel);
        //System.out.println(imgURL);
        //System.out.println(stu_id);
        //hp.sendRedirect("http://123.206.198.158/back.html");
    }

    //获得所有帖子
    @RequestMapping("/get_allPosts")
    public List<Map<String,Object>>do_getall(){
        return sv.getAllPost();
    }
    
   
    
    //根据传入的帖子号获得帖子对应的评论
    @RequestMapping("/get_comment")
    public List<Map<String, Object>> do_getcomment(@RequestParam("id") String id) {
        return sv.getcomment(id);
    }

    //根据传入的学生学号获取其昵称，若没有昵称，则为姓名
    @RequestMapping("/get_nickname")
    public String do_getnickname(@RequestParam("id") String id) {
        return sv.getnickname(id);
    }

    //根据传入的学生学号获取其头像的路径
    @RequestMapping("/get_stuPic")
    public String do_getstupic(@RequestParam("id") String id) {
        return sv.getstupic(id);
    }

    //根据传入的帖子号，评论人号，评论内容往某个特定的帖子插入评论
    @RequestMapping("/add_comment")
    public String do_addcomment(@RequestParam("id") String id, @RequestParam("stu_id") String stu_id, @RequestParam("chat") String chat) {
        sv.addcomment(id, stu_id, chat);
        return "true";
    }

    //往数据库中插入失物招领的信息,返回true(插入成功)/false(插入失败)
    @RequestMapping("/add_lostfound")
    public String do_addlostfound(@RequestParam("stu_id") String stu_id, @RequestParam("ttype") String ttype,
            @RequestParam("pic") String pic, @RequestParam("detail") String detail, @RequestParam("tel") String tel) {
        return sv.addlostfound(stu_id, ttype, pic, detail, tel);
    }

    //读出寻物/寻主的所有帖子
    @RequestMapping("/get_lostfound")
    public List<Map<String, Object>> do_getlostfound(@RequestParam("ttype") String ttype) {
        return sv.getlostfound(ttype);
    }

    //根据传入的学生学号获取其发布的所有帖子
    @RequestMapping("/get_message_byId")
    public List<Map<String, Object>> do_getmessagebyid(@RequestParam("id") String id) {
        return sv.getmessagebyid(id);
    }

    //根据传入的帖子号，帖子内容修改发布的消息
    @RequestMapping("/change_message")
    public void do_changemessage(@RequestParam("id") String id, @RequestParam("detail") String detail) {
        sv.changemessage(id, detail);
    }

    //根据传入的帖子号删除某条帖子
    @RequestMapping("/remove_message")
    public void do_removemessage(@RequestParam("id") String id) {
        sv.removemessage(id);
    }

}
